//
//  SpecailWebViewController.m
//  BeautyClother
//
//  Created by dllo on 15/8/27.
//  Copyright (c) 2015年 宋爽. All rights reserved.
//

#import "SpecailWebViewController.h"
#import "AsyncConnection.h"
#import "define.h"
#import "UIImageView+WebCache.h"
#import "SelectColorAndSizeViewController.h"
@interface SpecailWebViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *table;
@property (nonatomic, retain) NSMutableArray *array;
@property (nonatomic, retain) NSMutableDictionary *dataDic;
@property (nonatomic, retain) UIWebView *web;
@property (nonatomic, assign) CGFloat realHeight;
@property (nonatomic, assign) CGFloat offHeigt;
@property (nonatomic, assign) CGFloat offWidth;

@end

@implementation SpecailWebViewController


- (void)dealloc
{
    [_dataDic release];
    [_table release];
    [_array release];
    [_web release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.array = [NSMutableArray array];
        self.offHeigt = [[UIScreen mainScreen]bounds].size.height / 667;
        self.offWidth = [[UIScreen mainScreen]bounds].size.width / 375;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self beginConnection];
    // Do any additional setup after loading the view.
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"f.png"] style:UIBarButtonItemStyleDone target:self action:@selector(back:)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor darkGrayColor];
}

- (void)back:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)beginConnection
{
    NSDictionary *dic = @{@"business_id":@"203",
                          @"flag":@"",
                          @"sort":@"4"};
    http://api.mall.hichao.com/hichao/goods/sale_goods_list.php?business_id=409&flag=&sort=4
    [AsyncConnection asyncConnection:@"http://api.mall.hichao.com/hichao/goods/sale_goods_list.php" parmaters:dic block:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        self.dataDic = [[dic objectForKey:@"response"]objectForKey:@"data"];
        
        [self createTableView];
    }path:@"main_saleAndWeb"];
   
    
    
    
}

- (void)createTableView
{
    self.table = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    [self.view addSubview:self.table];
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.table release];
    [self createWeb];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3* _offHeigt;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 5* _offHeigt;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 500* _offHeigt;
    }
    if (indexPath.section == 1) {
        if (indexPath.row == 1) {
            return 100* _offHeigt;
        }else{
        return 50* _offHeigt;
        }
    }if (indexPath.section == 2) {
        return self.view.frame.size.height;
    }else
    {
        return 44* _offHeigt;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 1) {
        return 2;
    }else
    {
        return 1;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reuse"];
        
    }
    if (indexPath.section == 0) {
        if (self.view.frame.size.width * self.height / self.width > 400* _offHeigt ) {
            self.realHeight = 400* _offHeigt;
        }else
        {
            self.realHeight = self.view.frame.size.width * self.height / self.width;
        }

        UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.realHeight)];
                [cell.contentView addSubview:image];
        image.backgroundColor = [UIColor yellowColor];
        NSString *pic = [[[self.dic objectForKey:@"picUrl"]componentsSeparatedByString:@"?"]objectAtIndex:0];
        [image sd_setImageWithURL:[NSURL URLWithString:pic] placeholderImage:[UIImage imageNamed:@"zw.jpg"]];
        UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(10 *_offWidth, 400* _offHeigt, self.view.frame.size.width - 20*_offWidth, 50* _offHeigt)];
        title.text = [self.dic objectForKey:@"title"];
        [cell.contentView addSubview:title];
        title.text =[self.dic objectForKey:@"description"];
        UILabel *price = [[UILabel alloc]initWithFrame:CGRectMake(10*_offWidth, 450* _offHeigt, 140*_offWidth, 50* _offHeigt)];
        price.textColor = [UIColor colorWithRed:255/255 green:0 blue:255/255 alpha:0.8];
        price.font = [UIFont boldSystemFontOfSize:20];
        UILabel *priceOrig = [[UILabel alloc]initWithFrame:CGRectMake(140*_offWidth, 450* _offHeigt, 80*_offWidth, 50* _offHeigt)];
        priceOrig.textColor = [UIColor darkGrayColor];
        priceOrig.font = [UIFont systemFontOfSize:12];
        price.text = [@"售价 ￥" stringByAppendingString:[self.dic objectForKey:@"price"]];
        for (NSString *key in self.dic) {
            if ([key isEqualToString:@"priceOrig"]) {
                NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:[self.dic objectForKey:@"priceOrig"]];
                [attri addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:NSMakeRange(0, ((NSString *)[self.dic objectForKey:@"priceOrig"]).length)];
                [attri addAttribute:NSStrikethroughColorAttributeName value:[UIColor darkGrayColor] range:NSMakeRange(0, ((NSString *)[self.dic objectForKey:@"priceOrig"]).length)];
                NSAttributedString *att = [[NSAttributedString alloc]initWithString:@"售价 ￥"];
                [attri insertAttributedString:att atIndex:0];
                [priceOrig setAttributedText:attri];
                [cell.contentView addSubview:priceOrig];
            }else
        {
            priceOrig.text = nil;
        }
        }
        
            //// 设置删除线
            
            
        [cell.contentView addSubview:price];
        UILabel *discount = [[UILabel alloc]initWithFrame:CGRectMake(250 *_offWidth, 465 * _offHeigt, 50 *_offWidth, 20 * _offHeigt)];
        for (NSString *key in [self.dic allKeys]) {
            if ([key isEqualToString:@"discount"]) {
        discount.backgroundColor = [UIColor colorWithRed:255/255 green:0 blue:255/255 alpha:0.8];
            }else
            {
                discount.backgroundColor = [UIColor whiteColor];
            }
        }
        
        discount.layer.cornerRadius = 5;
        discount.layer.masksToBounds = YES;
        discount.textAlignment = 1;
        discount.textColor = [UIColor whiteColor];
        [cell.contentView addSubview:discount];
        discount.text = self.discount;
        cell.backgroundColor = [UIColor whiteColor];
    }
    if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            [cell.imageView sd_setImageWithURL:[NSURL URLWithString:self.imageUrl] placeholderImage:[UIImage imageNamed:@"zw.jpg"]];
            cell.textLabel.text = self.name;
            
        }
        if (indexPath.row == 1) {
            cell.textLabel.text = self.desc;
            cell.textLabel.numberOfLines = 0;
        }
    }
    return cell;
}


- (NSMutableAttributedString *)xiahuaxian:(NSString *)string
{
    NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:string];
    [attri addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:NSMakeRange(0, string.length)];
    [attri addAttribute:NSStrikethroughColorAttributeName value:[UIColor grayColor] range:NSMakeRange(0, string.length)];
    return attri;
}

- (void)createWeb
{
    
    self.web = [[UIWebView alloc]initWithFrame:CGRectMake(0, 650* _offHeigt, self.view.frame.size.width, self.view.frame.size.height)];
    [self.table addSubview:_web];
    
    NSString *urlStr = [[NSString alloc]init];
    urlStr = [NSString stringWithFormat:@"%@%@%@%@%@", SPECAIL_WEB,@"?m=", @"goodsdetail",@"&sid=", self.wid];
    
    
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlStr]];
    [self.web loadRequest:request];
    [_web release];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
